package com.skit.mine.ui;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.skit.common.base.BaseActivity;
import com.skit.common.bean.StatusResult;
import com.skit.common.config.RetrofitClient;
import com.skit.common.config.RouteConfig;
import com.skit.common.http.HttpUtils;
import com.skit.common.utils.ToastUtils;
import com.skit.mine.R;
import com.skit.mine.RegisterViewHelper;
import com.skit.mine.databinding.UserRegisterBinding;
import com.skit.common.retrofit.UserProtocol;
import com.skit.mine.span.HtmlTextSpan;

import retrofit2.Call;


@Route(path = RouteConfig.UserRoute.USER_REGISTER)
public class RegisterActivity extends BaseActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        UserRegisterBinding binding = DataBindingUtil.setContentView(this, R.layout.user_register);
        RegisterViewHelper registerViewHelper = new ViewModelProvider(this).get(RegisterViewHelper.class);
        binding.setRegisterViewHelper(registerViewHelper);
        binding.setLifecycleOwner(this);
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(getDrawable(R.drawable.ic_keyboard_backspace_black_24dp));
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
//            getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        setTitle("");

        binding.setNextListener(v -> {
            ProgressDialog progressDialog = new ProgressDialog(this);
            progressDialog.setMessage("正在请求");
            progressDialog.setCancelable(false);
            progressDialog.show();

            UserProtocol userProtocol = RetrofitClient.getService(UserProtocol.class);
            Call<StatusResult<String>> statusResultCall = userProtocol.checkVerificationCode(binding.userEmail.getText().toString(), binding.userVerificationCode.getText().toString());
            HttpUtils.doRequest(statusResultCall, result -> {
                progressDialog.dismiss();
                if (result == null) {
                    ToastUtils.showToast("验证失败");
                    return;
                }
                if (result.isSuccess()) {//验证成功
                    ToastUtils.showToast(result.getMsg());
                    CompleteInfoActivity.startActivity(RegisterActivity.this, binding.userEmail.getText().toString(), binding.userPassword.getText().toString());
                } else {//验证失败
                    ToastUtils.showToast(result.getMsg());
                }
            });

        });

        binding.setVerificationCodeListener(v -> {
            ProgressDialog progressDialog = new ProgressDialog(this);
            progressDialog.setMessage("正在请求");
            progressDialog.setCancelable(false);
            progressDialog.show();
            
            UserProtocol userProtocol = RetrofitClient.getService(UserProtocol.class);
            Call<StatusResult<String>> statusResultCall = userProtocol.sendVerificationCode(binding.userEmail.getText().toString());
            HttpUtils.doRequest(statusResultCall, result -> {
                progressDialog.dismiss();
                if (result == null) {
//                    Exception e = new Exception(result.getMsg());
//                        if (result != null)
                    ToastUtils.showToast("请求失败");
                    return;
                }

                if (result.isSuccess()) {//请求成功
                    ToastUtils.showToast(result.getMsg());
                } else {
                    ToastUtils.showToast(result.getMsg());
                }

            });

        });


        //底部协议提示文字
        HtmlTextSpan htmlTextSpan = HtmlTextSpan.setHtmlText(binding.agreementTextView, R.string.user_agreement);
        htmlTextSpan.setOnClickListener(v -> {
            Toast.makeText(this, "点击协议", Toast.LENGTH_SHORT).show();
        });


    }
}
